home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d998.lha / Touch / Touch.c < prev    next >
C/C++ Source or Header  |  1994-04-05  |  9KB  |  374 lines

  1. /*
  2. Auto:        sc <file>
  3. */
  4.  
  5. /* $Revision Header built automatically *************** (do not edit) ************
  6. **
  7. ** © Copyright by GuntherSoft
  8. **
  9. ** File             : SnakeSYS:CPrgs/Utils/Touch.c
  10. ** Created on       : Monday, 26.07.93 22:15:08
  11. ** Created by       : Kai Iske
  12. ** Current revision : V1.3
  13. **
  14. **
  15. ** Purpose
  16. ** -------
  17. **   - Small touch command which will create a file, if it doesn`t
  18. **     exists yet.
  19. **
  20. ** Revision V1.3
  21. ** --------------
  22. ** created on Tuesday, 15.03.94 13:42:11  by  Kai Iske.   LogMessage :
  23. **  -*-  changed on Tuesday, 15.03.94 13:55:24  by  Kai Iske.   LogMessage :
  24. **   - For ExAll() a wrong buffer size was supplied; Whoops
  25. **  -*-  changed on Tuesday, 15.03.94 13:48:04  by  Kai Iske.   LogMessage :
  26. **   - Pattern buffer wasn`t large enough
  27. **   - For some calls (like AddPart) the length of the dest buffer
  28. **     was to small according to the allocation
  29. **  -*-  created on Tuesday, 15.03.94 13:42:11  by  Kai Iske.   LogMessage :
  30. **   - For ParsePattern, the FileName wasn`t converted to uppercase
  31. **
  32. ** Revision V1.2
  33. ** --------------
  34. ** created on Friday, 31.12.93 12:56:39  by  Kai Iske.   LogMessage :
  35. **   - Touch will issue a warning if no matching files could be
  36. **     found for a pattern
  37. **     (Suggested by : Dan Barret)
  38. **
  39. ** Revision V1.1
  40. ** --------------
  41. ** created on Wednesday, 08.12.93 22:04:39  by  Kai Iske.   LogMessage :
  42. **   - Reduced stack usage
  43. **   - Reduced executable size
  44. **   - Recompiled using SAS 6.50
  45. **   - Added CTRL-C checking
  46. **
  47. ** Revision V1.0
  48. ** --------------
  49. ** created on Monday, 26.07.93 22:15:08  by  Kai Iske.   LogMessage :
  50. **     --- Initial release ---
  51. **
  52. *********************************************************************************/
  53. #define REVISION "1.3"
  54. #define REVDATE  "15.03.94"
  55. #define REVTIME  "13:55:24"
  56. #define AUTHOR   "Kai Iske"
  57. #define VERNUM   1
  58. #define REVNUM   3
  59.  
  60.  
  61. #include    <string.h>
  62. #include    <stdlib.h>
  63. #include    <exec/types.h>
  64. #include    <exec/memory.h>
  65. #include    <dos/dos.h>
  66. #include    <dos/exall.h>
  67. #include    <proto/exec.h>
  68. #include    <proto/dos.h>
  69.  
  70.  
  71.  
  72.  
  73. /**********************************************************************/
  74. /*                           Version String                           */
  75. /**********************************************************************/
  76. static const char *MyVer    = "$VER: Touch "REVISION" ("REVDATE")\0";
  77.  
  78.  
  79.  
  80.  
  81. /**********************************************************************/
  82. /*                  Template for Commandline Parsing                  */
  83. /**********************************************************************/
  84. static const char *Template    = "FILES/M/A";
  85. enum    {FILE_ARG, LAST_ARG};
  86.  
  87.  
  88.  
  89.  
  90.  
  91. /**********************************************************************/
  92. /*                       The small main program                       */
  93. /**********************************************************************/
  94. ULONG __saveds main(void)
  95. {
  96.     struct    ExecBase    *SysBase;
  97.     struct    DosLibrary    *DOSBase;
  98.     struct    Process        *MyProc;
  99.     struct    RDArgs        *RDArgs;
  100.     struct    ExAllControl    *EAC;
  101.     struct    ExAllData    *EAD;
  102.     struct    DateStamp    DS;
  103.     APTR    *EAB,
  104.         *Args;
  105.     BPTR    OutHandle,
  106.         TouchFile;
  107.     char    **FileNameList    = NULL,
  108.         *MainBuffer,
  109.         *FileName,
  110.         *Pattern,
  111.         *TouchName;
  112.     WORD    FileNameType;
  113.     ULONG    MySig;
  114.     BOOL    GoOn,
  115.         Breaked        = FALSE;
  116.  
  117.         // Get Base of Exec
  118.  
  119.     SysBase    = *((struct ExecBase **)0x4L);
  120.  
  121.         // Don`t start from WB
  122.  
  123.     MyProc    = (struct Process *)FindTask(NULL);
  124.  
  125.     if(!MyProc->pr_CLI)
  126.     {
  127.         struct    WBStartup    *Msg;
  128.  
  129.         WaitPort(&MyProc->pr_MsgPort);
  130.         Msg    = (struct WBStartup *)GetMsg(&MyProc->pr_MsgPort);
  131.         Disable();
  132.         ReplyMsg((struct Message *)Msg);
  133.         return(0);
  134.     }
  135.  
  136.         // Get DOSBase
  137.  
  138.     if(!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 0)))
  139.         return(20);
  140.  
  141.         // Get handle to Output
  142.  
  143.     OutHandle = Output();
  144.  
  145.         // Check for System we`re running on
  146.  
  147.     if(((struct Library *)SysBase)->lib_Version < 37)
  148.     {
  149.         Write(OutHandle, "You must use KickStart 2.04 (37.175) or higher for Touch\n", 55);
  150.         CloseLibrary((struct Library *)DOSBase);
  151.         return(20);
  152.     }
  153.  
  154.         // Get current DateStamp
  155.  
  156.     DateStamp(&DS);
  157.  
  158.  
  159.         // Get buffer for filename etc.
  160.  
  161.     if((MainBuffer = AllocVec(((1026 * 4) + 4), MEMF_CLEAR)))
  162.     {
  163.             // Set up buffers
  164.  
  165.         FileName    = MainBuffer;
  166.         TouchName    = (FileName + 1026);
  167.         Pattern        = (TouchName + 1026);
  168.  
  169.             // Get Buffer for arguments
  170.  
  171.         if((Args = AllocVec((LAST_ARG * sizeof(ULONG)), MEMF_CLEAR)))
  172.         {
  173.                 // Parse Commandline
  174.  
  175.             if((RDArgs = ReadArgs((char *)Template, (LONG *)Args, NULL)))
  176.             {
  177.                     // Get buffer for ExAll
  178.  
  179.                 if((EAB = AllocVec((sizeof(struct ExAllData) * 20), MEMF_CLEAR)))
  180.                 {
  181.                         // Get ExAllControl Structure
  182.  
  183.                     if((EAC = AllocDosObject(DOS_EXALLCONTROL, NULL)))
  184.                     {
  185.                             // Get pointers to FileNames passed
  186.  
  187.                         FileNameList = Args[FILE_ARG];
  188.  
  189.                             // Loop for all filenames
  190.  
  191.                         while(FileNameList && *FileNameList && !Breaked)
  192.                         {
  193.                                 // Check for CTRL-C
  194.  
  195.                             MySig    = CheckSignal(SIGBREAKF_CTRL_C);
  196.                             if(!(MySig & SIGBREAKF_CTRL_C))
  197.                             {
  198.                                     // Copy current filename
  199.  
  200.                                 strcpy(FileName, *FileNameList);
  201.                                 strupr(FileName);
  202.  
  203.                                     // Create pattern
  204.  
  205.                                 if((FileNameType = ParsePatternNoCase(FilePart(FileName), Pattern, 2050)) != -1)
  206.                                 {
  207.                                         // Check pattern type
  208.  
  209.                                     if(FileNameType)
  210.                                     {
  211.                                             // Real pattern. Remove trailing name
  212.  
  213.                                         *(PathPart(FileName)) = '\0';
  214.  
  215.                                             // Try to get lock to directory
  216.  
  217.                                         if((TouchFile = Lock(FileName, ACCESS_READ)))
  218.                                         {
  219.                                             BOOL    DoneOnce;
  220.  
  221.                                                 // Set up ExAllControl
  222.  
  223.                                             EAC->eac_LastKey    = 0;
  224.                                             EAC->eac_MatchString    = Pattern;
  225.                                             EAC->eac_MatchFunc    = NULL;
  226.  
  227.                                             DoneOnce    = FALSE;
  228.  
  229.                                             do
  230.                                             {
  231.                                                     // Check for CTRL-C
  232.  
  233.                                                 MySig    = CheckSignal(SIGBREAKF_CTRL_C);
  234.                                                 if((MySig & SIGBREAKF_CTRL_C))
  235.                                                     Breaked = TRUE;
  236.  
  237.                                                     // Loop directory
  238.  
  239.                                                 GoOn = ExAll(TouchFile, (struct ExAllData *)EAB, (sizeof(struct ExAllData)*20), ED_NAME, EAC);
  240.  
  241.                                                     // Error occured ???
  242.  
  243.                                                 if((!GoOn) && (IoErr() != ERROR_NO_MORE_ENTRIES))
  244.                                                     PrintFault(IoErr(), "Touch ");
  245.  
  246.                                                     // End of dir reached ;
  247.  
  248.                                                 if(EAC->eac_Entries == 0)
  249.                                                 {
  250.                                                     if(!DoneOnce)
  251.                                                     {
  252.                                                         strcpy(TouchName, "Touch : No pattern match for ");
  253.                                                         strcat(TouchName, *FileNameList);
  254.                                                         strcat(TouchName, "\n");
  255.                                                         FPuts(OutHandle, TouchName);
  256.                                                     }
  257.                                                     GoOn = FALSE;
  258.                                                 }
  259.                                                 else if(!Breaked)
  260.                                                 {
  261.                                                     DoneOnce    = TRUE;
  262.  
  263.                                                         // Get buffer to ExAll Buffer
  264.  
  265.                                                     EAD = (struct ExAllData *)EAB;
  266.  
  267.                                                     do
  268.                                                     {
  269.                                                             // Check for CTRL-C
  270.  
  271.                                                         MySig    = CheckSignal(SIGBREAKF_CTRL_C);
  272.                                                         if((MySig & SIGBREAKF_CTRL_C))
  273.                                                             Breaked = TRUE;
  274.                                                         else
  275.                                                         {
  276.  
  277.                                                                 // Clear Touchname and create new one
  278.  
  279.                                                             TouchName[0]    = '\0';
  280.                                                             AddPart(TouchName, FileName, 1024);
  281.                                                             AddPart(TouchName, EAD->ed_Name, 1024);
  282.  
  283.                                                                 // Set new date
  284.  
  285.                                                             SetFileDate(TouchName, &DS);
  286.  
  287.                                                                 // Loop for entries
  288.  
  289.                                                             EAD = EAD->ed_Next;
  290.                                                         }
  291.                                                     } while(EAD && !Breaked);
  292.                                                 }
  293.                                             } while(GoOn);
  294.  
  295.                                                 // UnLock directory
  296.  
  297.                                             UnLock(TouchFile);
  298.                                         }
  299.                                     }
  300.                                     else
  301.                                     {
  302.                                             // File to be touched there ???
  303.  
  304.                                         if((TouchFile = Lock(FileName, ACCESS_READ)))
  305.                                         {
  306.                                                 // Unlock it
  307.  
  308.                                             UnLock(TouchFile);
  309.  
  310.                                                 // ... and set filedate
  311.  
  312.                                             SetFileDate(FileName, &DS);
  313.                                         }
  314.                                             // Otherwise create new file
  315.  
  316.                                         else if((TouchFile = Open(FileName, MODE_NEWFILE)))
  317.                                             Close(TouchFile);
  318.                                     }
  319.                                 }
  320.                                 else
  321.                                 {
  322.                                     PrintFault(IoErr(), "Touch ");
  323.                                     FileNameList = NULL;
  324.                                 }
  325.                                     // Loop for files
  326.  
  327.                                 FileNameList++;
  328.                             }
  329.                             else
  330.                                 Breaked = TRUE;
  331.                         }
  332.  
  333.                         FreeDosObject(DOS_EXALLCONTROL, EAC);
  334.                     }
  335.                     else
  336.                         PrintFault(IoErr(), "Touch ");
  337.  
  338.                     FreeVec(EAB);
  339.                 }
  340.                 else
  341.                     FPuts(OutHandle, "Touch : Buffer for Directory Scan could not be allocated\n");
  342.  
  343.                 FreeArgs(RDArgs);
  344.             }
  345.             else
  346.                 PrintFault(IoErr(), "Touch ");
  347.  
  348.             FreeVec(Args);
  349.         }
  350.         else
  351.             FPuts(OutHandle, "Touch : Buffer for Commandline could not be allocated\n");
  352.  
  353.         FreeVec(MainBuffer);
  354.     }
  355.     else
  356.         PrintFault(ERROR_NO_FREE_STORE, "Touch ");
  357.  
  358.         // Display break status if needed
  359.  
  360.     if(Breaked)
  361.         FPuts(OutHandle, "Touch : ^C...\n");
  362.  
  363.         // Close DOS
  364.  
  365.     CloseLibrary((struct Library *)DOSBase);
  366.  
  367.         // Return appropriate code
  368.  
  369.     if(FileNameList)
  370.         return(0);
  371.     else
  372.         return(20);
  373. }
  374.